/**
 * related to listRowTemplate.ui
 * 
 * @Author : chenglm
 * @Timestamp : 2016-07-15
 */
var root=ui("$");
var nf = sm("do_Notification");
var helperOpen = require("helper/open");
var helperOrder = require("helper/order");
var qqmath = require("util/qqmath");
var do_Label_num = ui("do_Label_num");
var do_ALayout_order = ui("do_ALayout_order");
var do_ImageView_img = ui("do_ImageView_img");
var do_Label_title = ui("do_Label_title");
var do_Label_time = ui("do_Label_time");
var do_Label_orderStatus = ui("do_Label_orderStatus");
var do_Label_total = ui("do_Label_total");
var do_Button_pay = ui("do_Button_pay");
var do_Button_cancel = ui("do_Button_cancel");
var do_Button_comment = ui("do_Button_comment");
var do_Button_complete = ui("do_Button_complete");
var do_Button_returnPay = ui("do_Button_returnPay");
var layoutBtn = ui("layoutBtn");
//设置数据绑定的映射关系
root.setMapping({
	"do_Label_num.tag":"orderNo",
	"layoutBtn.tag":"orderType",
	"do_ImageView_img.source":"pathImg",
	"do_Label_title.text":"title",
	"do_Label_title.tag":"orderType",
	"do_Label_time.text":"strNewAt",
	"do_Label_orderStatus.tag":"orderStatus",
	"do_Label_total.text":"total",
	"do_Button_pay.tag":"payStatus",
	"do_Button_comment.tag":"commentStatus",
	"do_Button_complete.tag" : "deliveryStatus",
	"do_Label_10.text" :"sellerNick",
	"do_Button_cancel.visible" : "showCancel",
	"do_Button_cancel.tag" : "cancelEnable",
	"do_Button_pay.visible" : "showPay",
	"do_Button_comment.visible" : "showFeedback",
	"do_Button_complete.visible" : "showComplete",
	"do_Label_orderStatus.text" : "orderStatusText",
	//"layoutBtn.visible" : "hasBtn",
	"do_Button_returnPay.visible" : "showReturnPay",
	"do_ALayout_order.tag" : "returnPayStatus"
	
});

root.on("dataRefreshed", function(){
	do_Label_num.text = "No."+do_Label_num.tag;
	if (layoutBtn.tag == 1) {//家政订单
		do_Button_complete.text = "确认服务";
	}
	do_Label_total.text = "总计：￥" + qqmath.getDight(do_Label_total.text,2);
});

do_ALayout_order.on("touch", function(data){
	var orderNo = do_Label_num.tag;
	helperOpen.open("source://view/order/detail.ui", {orderNo : orderNo});
});

do_Button_cancel.on("touch", function(data){
	var orderNo = do_Label_num.tag;
	var orderType = layoutBtn.tag;
	var payStatus = do_Button_pay.tag;
	var cancelEnable = do_Button_cancel.tag;
	var msg = "确定取消订单?";
	if (orderType == 1) {
		msg = "服务前2小时外取消订单全额退款，2小时内取消将退款50%";
	}
	nf.confirm(msg, "", "确定", "我再想想", function(data, e) {
		if (data == 1) {
			if(layoutBtn.tag == 1){
				helperOrder.buyerCancel(orderNo,function (){
					do_Label_orderStatus.text = "已取消";
					do_Button_cancel.visible = false;
					do_Button_pay.visible = false;
				});
			}else if(layoutBtn.tag == 2 || layoutBtn.tag == 6){
				//商城\圈商订单
				if(cancelEnable == "true" || cancelEnable == 1){
					helperOrder.cancelMallOrder(orderNo,function (){
						do_Label_orderStatus.text = "已取消";
						do_Button_cancel.visible = false;
						do_Button_pay.visible = false;
					});
				}
			}else if(layoutBtn.tag == 4){//活动
				helperOrder.buyerCancel(orderNo,function (){
					do_Label_orderStatus.text = "已取消";
					do_Button_cancel.visible = false;
					do_Button_pay.visible = false;
				});
			}else if(layoutBtn.tag == 3){//闲置品
				helperOrder.buyerCancel(orderNo,function (){
					do_Label_orderStatus.text = "已取消";
					do_Button_cancel.visible = false;
				});
			}else if(layoutBtn.tag == 5){//代收代送
				helperOrder.buyerCancel(orderNo,function (){
					do_Label_orderStatus.text = "已取消";
					do_Button_cancel.visible = false;
				});
			}
		}
	});
});

do_Button_pay.on("touch", function(data){
	if (do_Button_pay.tag == 1) {
		var orderNo = do_Label_num.tag;
		helperOrder.toPay(orderNo);
	}
});

do_Button_comment.on("touch", function(data){
	var orderNo = do_Label_num.tag;
	if(layoutBtn.tag!=4){
		helperOpen.open("source://view/order/comment/listAdd.ui", {orderNo : orderNo});
	}
});

do_Button_complete.on("touch", function(data){
	var msg ="";
	if(do_ALayout_order.tag == 2){//退款中确认收货
		msg = "取消退款申请，确认收货？"
	}else{
		msg = "确认已收到商品？";
	}
	if (do_Button_complete.tag == 2) {
		nf.confirm(msg, "", "确定", "取消", function(data, e) {
			if (data == 1) {
				// 确认收货
				var orderNo = do_Label_num.tag;
				helperOrder.buyerSign(orderNo,function (data){
					do_Label_orderStatus.text = "已完成";
					do_Button_cancel.visible = false; // 隐藏取消按钮
					do_Button_complete.visible = false;
					if(ui("do_Label_title").tag!=5&&ui("do_Label_title").tag!=4){
						do_Button_comment.visible=true;
						helperOpen.open("source://view/order/comment/listAdd.ui", {orderNo : orderNo});
					}
				});
			}
		});
	}
});

do_Button_returnPay.on("touch", function(data){
	var payStatus = do_Button_pay.tag;
	if (payStatus == 2) {
		nf.confirm("您的退款申请将提交给商家，建议联系商家协商处理。如因您自身原因导致的申请，商家和客服有权拒绝。", "", "确定", "取消", function(data, e) {
			if (data == 1) {
				var orderNo = do_Label_num.tag;
				helperOrder.applyReturnPay(orderNo,function (){
					do_Label_orderStatus.text = "申请退款";
					do_Button_returnPay.visible = false;
					do_Button_cancel.visible = false;
					do_Button_complete.visible = false;
					//nf.toast("已提交申请，等待卖家审核");
				});
			}
		});
	}
});